﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Xml.Serialization;
using WXY.Course.BLL;
using WXY.Course.Model;

namespace WXY.Course.Web.Controllers
{
    public class HomeController : Controller
    {
        private ResourceBLL _resourceBLL = new ResourceBLL();
        private RecommendBLL _recommendBLL = new RecommendBLL();
        private NoticeBLL _noticeBLL = new NoticeBLL();

        public ActionResult Index()
        {
            ViewData["LeaderInfo"] = GetLeaderInfo();
            ViewData["RecommendedResource"] = _resourceBLL.GetRecommended();
            ViewData["Notice"] = _noticeBLL.GetList().FirstOrDefault(m => m.IsActive);
            ViewData["RecommendList"] = _recommendBLL.GetList();
            return View();
        }

        private LeaderInfo GetLeaderInfo()
        {
            XmlSerializer serializer = new XmlSerializer(typeof(LeaderInfo));
            string path = Server.MapPath("~/Leader.xml");
            LeaderInfo leader = null;
            using (FileStream fs = new FileStream(path, FileMode.Open))
            {
                using (StreamReader sr = new StreamReader(fs, Encoding.UTF8))
                {
                    leader = serializer.Deserialize(sr) as LeaderInfo;
                }
            }

            return leader;
        }
    }
}
